package com.limox.bishe.viewgroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListView;
import android.widget.Toast;

import com.limox.bishe.R;
import com.limox.bishe.activity.ClassroomActivity;
import com.limox.bishe.adapter.kbinfoAdapter;
import com.limox.bishe.app.APP;
import com.limox.bishe.db.DataBaseManager;
import com.limox.bishe.diyui.diyExpanableListview;

public class kbView {

	private Context context;
	private ListView lv;
	public View mainView;
	private String title;
	private Boolean isNowDay=false;

	public kbView() {

	}

	public kbView(Context context, String DAY) {
		this.context = context;

		mainView = LayoutInflater.from(context).inflate(R.layout.kb_list, null);

		// 从数据库中读取课表信息（根据时间）
		DataBaseManager.CreateDatabase(context, "kb", null);
		SQLiteDatabase SQLdb = DataBaseManager.OpenDb(context, "kb", null, 3);

		// 根据周几选择
		Time time = new Time();
		time.setToNow();
		String Day = "一";
		switch (time.weekDay) {
		case 1:
			Day = "一";
			break;
		case 2:
			Day = "二";
			break;
		case 3:
			Day = "三";
			break;
		case 4:
			Day = "四";
			break;
		case 5:
			Day = "五";
			break;
		case 6:
			Day = "六";
			break;
		case 0:
			Day = "日";
			break;

		default:
			break;
		}

		if(Day.equals(DAY)){
			isNowDay=true;
		}
		
		// 根据周几从数据库中查找课表
		Cursor cursor = DataBaseManager.select_kb(SQLdb, DAY);

		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		while (cursor.moveToNext()) {
			// 把cursor 转为arrarylist
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("cour_name", cursor.getString(0));
			map.put("teacher", cursor.getString(1));
			map.put("classroom", cursor.getString(2));
			map.put("week", cursor.getString(3));
			map.put("remark", cursor.getString(4));
			map.put("time", cursor.getString(5)); 
			mylist.add(map);
			
		}

		// 将mylist数据分析整理为两层数据，一层是expandablelistview的父列表，一层是expandablelistview的子列表
		// 显示课表信息
		initView(mylist);

	}

	/**
	 * 初始化界面
	 */
	public void initView(ArrayList<HashMap<String, String>> arraylist) {
		// 分析得出父列表的数据
		// 父list的数据
		ArrayList<HashMap<String, String>> grouplist = new ArrayList<HashMap<String, String>>();
		// 子list的数据
		List<ArrayList<HashMap<String, String>>> childlist = new ArrayList<ArrayList<HashMap<String, String>>>();

		for (int i = 0; i < APP.getJieCount(); i++) {
			int lessonCount=0;
			ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
 
			for (int j = 0; j < arraylist.size(); j++) {
 				String lessontimeStr = arraylist.get(j).get("time");
				int lessontime = Integer.valueOf(lessontimeStr.substring(
						lessontimeStr.indexOf("第") + 1,
						lessontimeStr.indexOf(",")));
				 
				if(lessontime==2*i+1){
					if(lessonCount==0){
	 					grouplist.add(arraylist.get(j));
	 					lessonCount++;
					}else{
						list.add(arraylist.get(j));
					} 
 				}else{
 					if(lessonCount==0&&j==arraylist.size()-1){
 						grouplist.add(null);
 					}
 				}

			}
			
  			 childlist.add(list);  
 
		}
  
		
		diyExpanableListview kblist = (diyExpanableListview) mainView
				.findViewById(R.id.kb_listview);
		kbinfoAdapter kbAdapter = new kbinfoAdapter(context, grouplist,
				childlist,isNowDay);
		kblist.setAdapter(kbAdapter);
		  
		//点击监听    
 		kblist.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
//				 
//				// TODO Auto-generated method stub
//				Intent intent = new Intent();
//				intent.setClass(context, ClassroomActivity.class);
//				context.startActivity(intent);
				return false;
			}
		});

	}

	public View getView() {
		return mainView;
	}

}
